#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

#include "ThreadPool.h"
#include "StdTcp.h"
#include "StdThread.h"
#include "DoubleLinkList.h"
#include "serveradmin.h"

C *Create(int nameid,int sock)
{
    C *c=(C *)malloc(sizeof(C));
    if(c==NULL)
    {
        perror("malloc");
        return NULL;
    }
    c->mynameid=nameid;
    c->sock=sock;
    c->onlineflag=1;
    return c;
}
void FreeC(C *c)
{
    free(c);
}
DLList ClinetList;

void *thread_headle(void *arg)
{
    int sock=*((int*)arg);

    Msg m;
    ServerUserLoginRegistration(sock,&m,&ClinetList);
    TcpServerRecv(sock,&m,sizeof(Msg));
    InsertTail(&ClinetList,Create(m.mynameid,sock));
    ServerAdminOperation(sock,&m,&ClinetList);
}

void main(int argc,char *argv[])
{
    if(argc!=3)
    {
        printf("invalid numbers");
        return;
    }
    ThreadP *p=InitThreadPool(100,10,50);
    if(p==NULL)
    {
        printf("threadpool is error\n");
        return ;
    }
    InitDLinkList(&ClinetList);
    TcpS *s=InitTcpServer(argv[1],atoi(argv[2]));
    int acceptsock;
    while((acceptsock=TcpServerAccept(s))>0)
    {
        ThreadP_AddTask(p,thread_headle,&acceptsock);
    }
}